#!/bin/sh

start_statsd() {
    if [ -z "$(pgrep -u statsd statsd)" ]; then
        pushd /home/statsd/statsd
        su statsd -c "nohup node stats.js config.js 2>&1 &"
        popd
    fi
}
stop_statsd() {
    pkill -u statsd statsd
}


if [ "$1" = "start" ]; then
    start_statsd
elif [ "$1" = "stop" ]; then
    stop_statsd
elif [ "$1" = "restart" ]; then
    stop_statsd
    sleep 1
    start_statsd
elif [ "$1" = "status" ]; then
    pid=$(pgrep -u statsd statsd)
    if [ -z "$pid" ]; then
        echo "statsd not running"
    else
        echo "statsd is running $pid"
    fi
else
    echo "Usage: $0 {start|stop|restart|status}"
fi
